package org.example.security.auth.config.authorize;

import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

import javax.annotation.Resource;

@EnableAuthorizationServer
//@Configuration
public class AuthorizationServerConfigUseRefresh extends AuthorizationServerConfigurerAdapter {

    @Resource
    @Lazy
    private PasswordEncoder passwordEncoder;

    @Resource
    private AuthenticationManager authenticationManager;

    @Resource
    private UserDetailsService userDetailsService;

    @Resource
    private TokenStore tokenStore;

    @Resource
    private JwtAccessTokenConverter jwtAccessTokenConverter;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        // 配置JWT的内容增强器，可以在payload中添加自定义数据
        //        List<TokenEnhancer> delegates = new LinkedList<>();
        //        delegates.add(new JwtTokenEnhancer());
        //        delegates.add(jwtAccessTokenConverter);
        //
        //        TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
        //        tokenEnhancerChain.setTokenEnhancers(delegates);

        // 密码模式需要配置
        endpoints.authenticationManager(authenticationManager)
                // token存储策略
                .tokenStore(tokenStore)
                .reuseRefreshTokens(false)
                .userDetailsService(userDetailsService)
                .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        // 允许表单认证
        security.allowFormAuthenticationForClients();
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        /*
            刷新令牌：refresh_token
            http://localhost:8080/oauth/token?grant_type=refresh_token&client_id=client&client_secret=123456&refresh_token={refresh_token值}
         */

        clients.inMemory()
                .withClient("client")
                .secret(passwordEncoder.encode("123456"))
                .redirectUris("http://www.baidu.com")
                .accessTokenValiditySeconds(3600)
                .refreshTokenValiditySeconds(86400)
                .scopes("all")
                // .autoApprove(true)
                .authorizedGrantTypes("refresh_token");
    }
}
